﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;

namespace WebApp.Management.Product
{
    public partial class cardnumberer : PageBase2
    {
        private CardNumbersManger m_cardNumgersManger = new CardNumbersManger();
        private RoleManager m_RoleManager = new RoleManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }
        }

        protected void btnInsertProduct_Click(object sender, EventArgs e)
        {
            cardnumber cardnumber = new cardnumber();
            if (string.IsNullOrEmpty(this.txtname.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请填写卡号!');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.TextBox1.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请填写密码!');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.TextBox2.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请填写此卡面值!');", true);
                return;
            }
            if (this.m_cardNumgersManger.CheckExists(this.txtname .Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('次卡号已经存在!');", true);
            }
            else
            {
                cardnumber.cardnumbers = txtname.Text.Trim();
            }
            cardnumber.balance =Convert.ToDecimal (TextBox2.Text.Trim());
            cardnumber.password = TextBox1.Text.Trim();
            cardnumber.Isuserd = false;
            cardnumber.ExpireTime = DateTime.Now;
            this.m_cardNumgersManger.Insert(cardnumber);
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('Insert Succeed!');", true);
            this.TextBox2.Text = "";
            this.txtname.Text = "";
            this.TextBox1.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.TextBox1.Text = "";
            this.TextBox2.Text = "";
            this.txtname.Text="";
        }
    }
}
